﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VhaBot.Net;
using VhaBot.Net.Events;

namespace AoCchatUI
{
    public partial class ChildChannelForm : Form
    {
        //ParentForm theParent;
        string channel;        
        Chat chat;

        public ChildChannelForm(Chat chat, string channel)
        {
            InitializeComponent();

            //theParent = (ParentForm)this.ParentForm;

            this.chat = chat;
            this.channel = channel;
        }

        private void ChildChannelForm_Load(object sender, EventArgs e)
        {
            splitContainer1.Panel2.Padding = new System.Windows.Forms.Padding(2, 0, 2, 0);
            splitContainer1.Panel1.Padding = new System.Windows.Forms.Padding(2, 0, 2, 0);

            textBox1.Focus();

            chat.ChannelMessageEvent += new ChannelMessageEventHandler(OnChannelMessageEvent);
            chat.PrivateMessageEvent += new PrivateMessageEventHandler(OnPrivateMessageEvent);
        }

        private void addText(string channel, string character, string msg, Color color)
        {
            // Add timestamp with color
            string time = ("[" + DateTime.Now.TimeOfDay.ToString().Substring(0, 8) + "]");
            richTextBox1.SelectionStart = richTextBox1.TextLength + time.Length;
            richTextBox1.SelectionColor = Color.Yellow;
            richTextBox1.SelectedText = time;

            //channel color
            richTextBox1.SelectionStart = richTextBox1.TextLength + channel.Length;
            richTextBox1.SelectionColor = Color.AntiqueWhite;
            richTextBox1.SelectedText = channel;

            //name color
            richTextBox1.SelectionStart = richTextBox1.TextLength + character.Length;
            richTextBox1.SelectionColor = Color.Yellow;
            richTextBox1.SelectedText = character;

            // msg color
            richTextBox1.SelectionStart = richTextBox1.TextLength + time.Length + channel.Length + character.Length;
            richTextBox1.SelectionColor = color;
            richTextBox1.SelectedText = msg + Environment.NewLine;
        }

        private void ParseInput()
        {
            string input = textBox1.Text;

            if (input.StartsWith("/"))
            {
                if (input == "/help")
                {
                    string help = "\r\nClient commands :\r\n /help\tWhat you are seeing now.\r\n" +
                                  "Ingame commands :\r\n !random\tRoll a dice.";
                    addText("", "", help, Color.Aqua);
                }
                else if (input.Substring(0,5) == "/tell")
                {
                    if (input.Length <= 5)
                    {
                        addText("", "", " Syntax : /tell [name] message", Color.Red);
                    }
                    else
                    {
                        string name = input.Split(' ')[1];
                        string msg = input.Substring(7 + name.Length);

                        chat.SendPrivateMessage(name, msg);
                    }
                }
                else
                {
                    addText("", "", " Unknown command.", Color.Red);
                }
            }
            else
            {                
                chat.SendChannelMessage(channel, input);             
            }

            textBox1.Clear();
            textBox1.Focus();
        }

        // Handle private messages
        private void OnPrivateMessageEvent(Chat chat, PrivateMessageEventArgs e)
        {
            if (chat.ID == e.CharacterID || e.Outgoing) return;

            addText("[Private]", "[" + e.Character + "]", ": " + e.Message, System.Drawing.Color.RoyalBlue);

            CommandArgs args = new CommandArgs(chat, true, e.CharacterID, e.Character, e.Message);
            Commands.OnCommand(chat, args);
        }

        // Handle channel messages
        private void OnChannelMessageEvent(Chat chat, ChannelMessageEventArgs e)
        {            
            if (e.Channel == channel)
            {
                //Item[] items = Item.ParseString(e.Message);                
               
                addText("[" + e.Channel + "]", "[" + e.Character + "]", ": " + e.Message, System.Drawing.Color.DarkOrange);

                if (channel == "~Guild")
                {
                    CommandArgs args = new CommandArgs(chat, false, e.CharacterID, e.Character, e.Message);
                    Commands.OnCommand(chat, args);
                }
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            // Tails the text
            richTextBox1.SelectionStart = richTextBox1.Text.Length;
            richTextBox1.ScrollToCaret();
            richTextBox1.Refresh();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData.ToString() == "Return")
            {
                ParseInput();
            }
        }
    }
}
